package 富婆梦.第一阶段.Java基础视频学习.NIO;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.Java基础视频学习.NIO
 * @ClassName: ChannelTest
 * @Author: hemengjin
 * @Description:
 * @Date:2021/11/28 16:00
 * @Version: 1.0
 */
public class ChannelTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile aFile = new RandomAccessFile("D:\\何梦锦的宝藏\\Java学习\\tata.txt", "rw");
        FileChannel fileChannel = aFile.getChannel();

        //分配缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(48);

        //fileChannel.read(buffer)返回写入的数据长度
        int bytesRead = fileChannel.read(buffer);


        while (bytesRead != -1) {
            System.out.println("read:" + bytesRead);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }
            buffer.clear();
            bytesRead = fileChannel.read(buffer);
        }
        fileChannel.close();
    }
}
